layui.use(['form','layer','laydate','table','laytpl'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laydate = layui.laydate,
        laytpl = layui.laytpl,
        table = layui.table;

    //菜单列表
    var tableIns = table.render({
        elem: '#baseMenuList',
        url : '/baseMenu/JsonList',
        cellMinWidth : 95,
        page : false,
        height : "full-125",
        limit : 200,
        method:'post',
        limits : [10,15,20,25],
        id : "baseMenuList",
        cols : [[
            {type: 'numbers', title: '序号'},
            {field: 'menuname', title: '菜单名称', width:200, templet:function(row){
                if(row.menugrade == "1"){
                    return row.menuname;
                }else{
                    return "<div style=' margin-left: 40px;'>"+row.menuname+"</div>";
                }
            }},
            {field: 'menusign', title: '菜单图标', align:'center',templet:function(d){
                return '<i class="layui-icon">'+d.menusign+'</i>';
            }},
            {field: 'menunum', title: '菜单排序',  align:'center'},
            {field: 'menuurl', title: '菜单连接', align:'center'},
            {field: 'createtime', title: '创建时间', align:'center', minWidth:110, templet:function(d){
                return d.createtime;
            }},
            {title: '操作',width:170, templet:'#baseMenuBar',align:"center"}
        ]]
    });

    //搜索
    $(".search_btn").on("click",function(){
        table.reload("baseMenuList",{
            page: false,
            where: {
                menuname: $(".searchVal").val()
            }
        })
    });

    //添加一级菜单
    function addEntity(params){
        var index = layui.layer.open({
            title : "一级菜单添加",
            type : 2,
            content : "/baseMenu/SelectEntity"+params,
            success : function(layero, index){
                var body = layui.layer.getChildFrame('body', index);
                body.find(".closeLay").bind("click",function () {
                    layui.layer.close(index);
                })
                setTimeout(function(){
                    layui.layer.tips('点击此处返回菜单列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500)
            }
        })
        layui.layer.full(index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize",function(){
            layui.layer.full(index);
        })
    }
    $(".add_btn").click(function(){
        addEntity('');
    })

    //列表操作
    table.on('tool(baseMenuList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;
        if(layEvent === 'edit'){ //编辑
            addEntity("?menuId="+data.menuid);
        } else if(layEvent === 'del'){ //删除
            layer.confirm('是否删除菜单名为【'+data.menuname+'】目录及其子菜单？',{icon:3, title:'提示信息'},function(index){
                 $.get("/baseMenu/DeleteEntity",{
                     "menuId":data.menuid,
                     "_method":"DELETE"
                 },function(result){
                     if(1==result.returnInfo.state){
                         tableIns.reload();
                         layer.close(index);
                     }else{
                         top.layer.msg(result.returnInfo.errorMsg);
                     }
                 })
            });
        } else if(layEvent === 'addChildBtn'){ //预览
            addEntity("?parentId="+data.menuid);
        }
    });

})